package com.example.imooccommon.util;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Anita
 * @Date 2018/1/17 11:35
 */
public class PageUtils<T> {

    public Page<T> getPage(List<T> allList, int page, int size){
        int totalSize = allList.size();
        int index = page*size;
        if (totalSize == 0 || index > totalSize) {
            return new PageImpl<T>(new ArrayList<T>(), new PageRequest(page, size), totalSize);
        }
        int endIndex = index + size;
        if( endIndex > totalSize){
            endIndex = totalSize;
        }
        return new PageImpl<T>(allList.subList(index,endIndex),new PageRequest(page,size),totalSize);
    }
}
